package com.guet.niit.Login.controller.admin;

import com.guet.niit.Admin.dao.*;
import com.guet.niit.Admin.pojo.Agent;
import com.guet.niit.Admin.pojo.Charging;
import com.guet.niit.Admin.pojo.ChargingPile;
import com.guet.niit.Admin.pojo.User;
import com.guet.niit.util.adminSwitchoverMenu;
import com.guet.niit.util.exitLogin;
import com.sun.javafx.stage.StageHelper;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;

public class AdminMenuFxController implements Initializable {
    public static AdminMenuFxController adminMenuFxController;

    @FXML
    private MenuItem stationManage;

    @FXML
    private MenuItem stationPileManage;

    @FXML
    private MenuItem agentManage;

    @FXML
    private MenuItem userManage;

    @FXML
    private MenuItem exit;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        adminMenuFxController = this;
    }

    /**
     * 进入充电站管理
     * @param mouseEvent
     */
    public void stationManage(MouseEvent mouseEvent) {
        //引用fxml样式
        Parent load = null;
        try {
            load = FXMLLoader.load(getClass().getResource("/fxml/admin/chargingStationManage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取当前窗口，并将Scene更换为用户菜单的Scene
        ObservableList<Stage> stageList = StageHelper.getStages();
        stageList.get(0).setScene(new Scene(load));

        //调用dao层查询充电站表
        ChargingDaolmpl chargingDao = new ChargingDaolmpl();
        List<Charging> stations = chargingDao.chargingInquire();

        //读取充电站列表信息
        ChargingStationManageFxController.chargingStationManageFxController.readStationsInfo(stations);
    }

    /**
     * 进入充电桩管理
     * @param mouseEvent
     */
    public void stationPileManage(MouseEvent mouseEvent) {
        //引用fxml样式
        Parent load = null;
        try {
            load = FXMLLoader.load(getClass().getResource("/fxml/admin/chargingPileManage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取当前窗口，并将Scene更换为用户菜单的Scene
        ObservableList<Stage> stageList = StageHelper.getStages();
        stageList.get(0).setScene(new Scene(load));

        //调用dao层查询充电桩表
        ChargingPileDaolmpl chargingPileDao = new ChargingPileDaolmpl();
        List<ChargingPile> chargingPiles = chargingPileDao.chargingPileInquire();

        //读取充电桩列表信息
        ChargingPileManageFxController.chargingPileManageFxController.readStationPileInfo(chargingPiles);
    }

    /**
     * 进入代理商管理
     * @param mouseEvent
     */
    public void agentManage(MouseEvent mouseEvent) {
        //引用fxml样式
        Parent load = null;
        try {
            load = FXMLLoader.load(getClass().getResource("/fxml/admin/agentManage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取当前窗口，并将Scene更换为用户菜单的Scene
        ObservableList<Stage> stageList = StageHelper.getStages();
        stageList.get(0).setScene(new Scene(load));

        //调用dao层查询代理商表
        AgentDaolmpl agentDao = new AgentDaolmpl();
        List<Agent> agents = agentDao.agentInquire();

        //读取代理商列表信息
        AgentManageFxController.agentManageFxController.readAgentInfo(agents);
    }

    /**
     * 进入用户管理
     * @param mouseEvent
     */
    public void userManage(MouseEvent mouseEvent) {
        //引用fxml样式
        Parent load = null;
        try {
            load = FXMLLoader.load(getClass().getResource("/fxml/admin/userManage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取当前窗口，并将Scene更换为用户菜单的Scene
        ObservableList<Stage> stageList = StageHelper.getStages();
        stageList.get(0).setScene(new Scene(load));

        //调用dao层查询用户表
        UserDaolmpl userDao = new UserDaolmpl();
        List<User> users = userDao.userInquire();

        //读取用户列表信息
        UserManageFxController.userManageFxController.readUserInfo(users);
    }

    /**
     * 通过菜单子项进入充电站管理
     * @param actionEvent
     */
    public void enterStationManageByMenuItem(ActionEvent actionEvent) {
        String text = stationManage.getText();
        adminSwitchoverMenu.backtrack(AdminMenuFxController.class,text);
        //调用dao层查询充电站表
        ChargingDaolmpl chargingDao = new ChargingDaolmpl();
        List<Charging> stations = chargingDao.chargingInquire();
        //读取充电站列表信息
        ChargingStationManageFxController.chargingStationManageFxController.readStationsInfo(stations);
    }

    /**
     * 通过菜单子项进入充电桩管理
     * @param actionEvent
     */
    public void enterStationPileManageByMenuItem(ActionEvent actionEvent) {
        String text = stationPileManage.getText();
        adminSwitchoverMenu.backtrack(AdminMenuFxController.class,text);
        //调用dao层查询充电桩表
        ChargingPileDaolmpl chargingPileDao = new ChargingPileDaolmpl();
        List<ChargingPile> chargingPiles = chargingPileDao.chargingPileInquire();

        //读取充电桩列表信息
        ChargingPileManageFxController.chargingPileManageFxController.readStationPileInfo(chargingPiles);
    }

    /**
     * 通过菜单子项进入代理商管理
     * @param actionEvent
     */
    public void enterAgentManageByMenuItem(ActionEvent actionEvent) {
        String text = agentManage.getText();
        adminSwitchoverMenu.backtrack(AdminMenuFxController.class,text);
        //调用dao层查询代理商表
        AgentDaolmpl agentDao = new AgentDaolmpl();
        List<Agent> agents = agentDao.agentInquire();

        //读取代理商列表信息
        AgentManageFxController.agentManageFxController.readAgentInfo(agents);
    }

    /**
     * 通过菜单子项进入用户管理
     * @param actionEvent
     */
    public void enterUserManageByMenuItem(ActionEvent actionEvent) {
        String text = userManage.getText();
        adminSwitchoverMenu.backtrack(AdminMenuFxController.class,text);
        //调用dao层查询用户表
        UserDaolmpl userDao = new UserDaolmpl();
        List<User> users = userDao.userInquire();

        //读取用户列表信息
        UserManageFxController.userManageFxController.readUserInfo(users);
    }

    /**
     * 点击菜单项的退出项退出系统
     * @param actionEvent
     */
    public void clickExit(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要退出吗？");
        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == ButtonType.OK){
//            Platform.exit();
            exitLogin.backtrack(AdminMenuFxController.class);
        }else {
            alert.close();
        }
    }

    /**
     * 关于我们
     * @param actionEvent
     */
    public void aboutUs(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("关于我们");
        alert.setContentText("充电桩管理系统由Power-station项目组人员开发，本管理系统目前已实现四个模块的增删改查。");
        alert.showAndWait();
    }
}
